﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace Kugar.Core.Data
{
    public class VM_PagedDataTable
    {
        public VM_PagedDataTable(PagingInfo pageInfo,DataTable data)
        {
            PageInfo = pageInfo;
            Table = data;
        }

        public PagingInfo PageInfo { private set; get; }

        public DataTable Table { set; get; }

    }

    public class VM_PagedObjectList<T>
    {
        public VM_PagedObjectList(PagingInfo pageInfo, IEnumerable<T> data)
        {
            PageInfo = pageInfo;
            Table = data;
        }

        public PagingInfo PageInfo { private set; get; }

        public IEnumerable<T> Table { private set; get; }
    }

    public class VM_PagedDataReader
    {
        public VM_PagedDataReader(PagingInfo pageInfo, DbDataReader data)
        {
            PageInfo = pageInfo;
            Table = data;
        }

        public PagingInfo PageInfo { private set; get; }

        public DbDataReader Table { set; get; }
    }

    public class PagingInfo
    {
        public int PageIndex { set; get; }

        public int PageSize { set; get; }

        public int PageCount {protected set; get; }

        private int _recordCount = 0;
        public int RecordCount
        {
            get { return _recordCount; }
            set
            {
                _recordCount = value;

                if ((value % PageSize) > 0)
                    PageCount = value / PageSize + 1;
                else
                    PageCount = value / PageSize;
            }
        }

    }

    public class PagingArgs : PagingInfo
    {
        #region "默认主键配置"
        private static string s_DefaultKeyField = "FId";

        /// <summary>获取或设置默认的主键字段</summary>
        public static string DefaultKeyFiled
        {
            get { return s_DefaultKeyField; }
            set { s_DefaultKeyField = value; }
        }
        #endregion

        private string _KeyField = DefaultKeyFiled;
        /// <summary>获取或设置主键字段名(默认为 "FId")</summary>
        public string KeyField
        {
            get { return _KeyField; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentException("需要提供主键");
                _KeyField = value;
            }
        }

        private string _columnSql = "*";

        /// <summary>获取或设置要读取的字段(默认为"*")</summary>
        public string ColumnSql
        {
            get
            {
                if (string.IsNullOrEmpty( _columnSql))
                {
                    return "*";
                }
                else
                {
                    return _columnSql;
                }
            }
            set
            {
                if (_columnSql!=value)
                {
                    _columnSql = value;
                }
            }
        }

        /// <summary>获取或设置要读取的表名，视图，或者SQL语句</summary>
        public string TableSql { get; set; }

        /// <summary>获取或设置筛选条件SQL语句</summary>
        public string WhereSql { get; set; }

        /// <summary>获取或设置排序SQL语句</summary>
        public string OrderSql { get; set; }

        public void EnsurePageIndex()
        {
            EnsurePageIndex(this);
        }

        /// <summary>
        /// 确保页码分页参数中的PageIndex在有效访问
        /// </summary>
        /// <param name="args">分页参数</param>
        /// <exception cref="ArgumentNullException" />
        public static void EnsurePageIndex(PagingArgs args)
        {
            if (args == null) throw new ArgumentNullException("args");

            if (args.PageCount == 0)
            {
                args.PageIndex = 0;
            }
            else if (args.PageIndex > args.PageCount)
            {
                args.PageIndex = args.PageCount;
            }
            else if (args.PageIndex < 1)
            {
                args.PageIndex = 1;
            }
        }
    }
}

